<?php
	$AppUI->savePlace();

	require_once("classes/CustomFields.class.php");

	$titleBlock = new CTitleBlock('Custom field editor', 'customfields.png');
	$titleBlock->addButton( 'Main page', '?m=system' );

	$edit_field_id = dpGetParam( $_POST, 'field_id', NULL );

	$titleBlock->show();

	$sql = "SELECT * FROM modules WHERE mod_name IN ('Companies', 'Projects', 'Tasks', 'Calendar') ORDER BY mod_ui_order";
	$modules = db_loadList( $sql );
?>
<script>
function doCheckFieldType(sel)
{
	if (sel.options[sel.selectedIndex].value == 'select') {
		Control('idSelectAttributes').show();
		$('idShadow').style.height = $('idCustomFieldDialog').offsetHeight + 'px';
	}
	else {
		Control('idSelectAttributes').hide();
		$('idShadow').style.height = $('idCustomFieldDialog').offsetHeight + 'px';
	}
}

function doAddItem()
{
	var f = $('idNewItem');
	var t = $('idTableItem');
	var r = t.insertRow(0);
	var c = r.insertCell(0);
	c.innerHTML = '<img src="images/common_delete.gif" onclick="doDeleteItem(this)" class="clickable"/>&nbsp;' +
	   	f.value + '<input type="hidden" name="select_items[]" value="'+f.value+'"/>';
}

function doDeleteItem(m)
{
	var t = $('idTableItem');
	var r = m.parentNode.parentNode;
	for (i in t.rows) {
		if (t.rows[i] == r) {
			t.deleteRow(i);
			break;
		}
	}
}

function doSaveCustomField()
{
	frm = document.custform;
	frm.action = '?m=system&a=custom_field_editor';
	$('dosql').name = 'dosql';
	frm.submit();
}

function doEditCustomField(mod, id) {
	var f = id != 0 ? '&field_id='+id : '';
	ajax.send('index.php?m=system&a=custom_field_addedit&suppressHeaders=1&module='+mod+f,'',
		function(r) {
			$('idCustomFieldDialog').innerHTML = r;
			Dialog('idCustomFieldDialog').show();
		}
	);
}

function doDeleteCustomField(h, id) {
	if (!confirm('Are you sure you want to delete select item?'))
	   return;

	ajax.send('index.php?m=system&a=custom_field_addedit&suppressHeaders=1&field_id='+id+'&delete=1','',
		function(r) {
			$('idCustomFieldDialog').innerHTML = r;
			Dialog('idCustomFieldDialog', 3).show();

			r = h.parentNode.parentNode;
			t = r.parentNode;
			for (i in t.rows) {
				if (t.rows[i] == r) {
					t.deleteRow(i);
					break;
				}
			}
		}
	);
}

function doCloseDialog() {
	Dialog('idCustomFieldDialog').hide();
}
</script>
<div id="idCustomFieldDialog" class="popup" style="visibility: hidden" autohide="click"></div>
<table cellpadding="2">
<?
foreach ($modules as $module) {
	$sql = 'SELECT * FROM custom_fields_struct WHERE field_module = "'.strtolower($module['mod_name']).'"';
	$custom_fields = db_loadList( $sql );
?>
<tr>
	<td>
	<table id="idTable<?=$module['mod_name']?>" border="0" cellspacing="0" cellpadding="3" width="400px" class="list">
	<tr>
		<td colspan="3">
			<a href="javascript:doEditCustomField('<?=$module['mod_name']?>', 0)"
				title="<?=$AppUI->_('Add a new Custom Field to '.$module['mod_name'].' Module')?>"><img src="images/common_add.gif"></a>
			&nbsp;&nbsp;
			<b><?=$AppUI->_($module['mod_name'])?></b>
		</td>
	</tr>
	<?
	if (!count($custom_fields)) {
		showEmptySpanRow('padding');
	}
	else
	foreach ($custom_fields as $f) {
	?>
	<tr class="second">
		<td>
			<a href="javascript:doEditCustomField('<?=$module['mod_name']?>', <?=$f['field_id']?>)"
				title="Edit"><img src="images/edit.gif"></a>
		</td>
		<td width="100%"><?=stripslashes($f['field_description'])?></td>
		<td>
			<img class="clickable" src="images/common_delete.gif" title="Delete"
				onclick="doDeleteCustomField(this, <?=$f['field_id']?>)"/>
		</td>
	</tr>
	<? } ?>
	</table>
	<script>Table('idTable<?=$module['mod_name']?>').setSelectiveHover(1, ['second'])</script>
	<td>
</tr>
<? } ?>
</table>
